package com.example.day1;

import android.content.Context;
import android.util.Log;
import android.webkit.WebSettings;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.HttpException;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
    private static RetrofitClient mInstance;
    private Retrofit retrofit;
    private Context mContext;
    private ApiServer apiServer;

    public void initRetrofit(Context context){
        mContext = context;

        if(retrofit == null){
            retrofit = new Retrofit.Builder()
                    .client(getOkHttpClient())
                    .baseUrl("https://cloud.anylink.io:8443/")
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

    }

    private RetrofitClient() {
    }

    public ApiServer getApiServer() {
        if (apiServer == null) {
            synchronized (ApiServer.class) {
                if (apiServer == null) {
                    apiServer = getRetrofit().create(ApiServer.class);
                }
            }
        }
        return apiServer;
    }

    public static RetrofitClient getInstance() {
        if (mInstance == null) {
            synchronized (RetrofitClient.class) {
                if (mInstance == null) {
                    mInstance = new RetrofitClient();
                }
            }
        }
        return mInstance;
    }

    private OkHttpClient getOkHttpClient(){
        return new OkHttpClient().newBuilder()
                .readTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .addInterceptor(new HeaderInterceptor())
                .build();

    }

    public Retrofit getRetrofit(){
        return retrofit;
    }

    private  class HeaderInterceptor implements Interceptor {

        @NonNull
        @Override
        public Response intercept(@NonNull Chain chain) throws IOException {

            Request.Builder builder = chain.request().newBuilder();
            builder.addHeader("User-Agent", WebSettings.getDefaultUserAgent(mContext));
            builder.addHeader("Content-Type", "application/json;charset=UTF-8");
            builder.addHeader("token", "");
            return chain.proceed(builder.build());
        }
    }

    private HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {

        @Override
        public void log(@NonNull String s) {
            Log.i("TAG", s);
        }
    });
}
